package cn.wjssl.test.domain;

import cn.wjssl.domain.activity.model.entity.PartakeRaffleActivityEntity;
import cn.wjssl.domain.activity.model.entity.SkuRechargeEntity;
import cn.wjssl.domain.activity.model.entity.UnpaidActivityOrderEntity;
import cn.wjssl.domain.activity.model.entity.UserRaffleOrderEntity;
import cn.wjssl.domain.activity.model.valobj.OrderTradeTypeVO;
import cn.wjssl.domain.activity.service.IRaffleActivityAccountQuotaService;
import cn.wjssl.domain.activity.service.IRaffleActivityPartakeService;
import cn.wjssl.domain.activity.service.armory.IActivityArmory;
import cn.wjssl.domain.credit.model.entity.TradeEntity;
import cn.wjssl.domain.credit.model.valobj.TradeNameVO;
import cn.wjssl.domain.credit.model.valobj.TradeTypeVO;
import cn.wjssl.domain.credit.service.ICreditAdjustService;
import cn.wjssl.trigger.api.IRaffleActivityService;
import cn.wjssl.trigger.api.dto.SkuProductShopCartRequestDTO;
import cn.wjssl.types.exception.AppException;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.concurrent.CountDownLatch;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class ActivityTest {

    @Resource
    private IRaffleActivityAccountQuotaService accountQuotaService;

    @Resource
    private ICreditAdjustService creditAdjustService;

    @Resource
    private IActivityArmory armory;

    @Resource
    private IRaffleActivityService activityService;

    @Test
    public void initTest() {

        SkuRechargeEntity skuRechargeEntity = new SkuRechargeEntity();
        skuRechargeEntity.setUserId("lxd");
        skuRechargeEntity.setSku(9011L);
        // outBusinessNo 作为幂等仿重使用，同一个业务单号2次使用会抛出索引冲突 Duplicate entry '800091009111' for key 'uq_out_business_no' 确保唯一性。
        skuRechargeEntity.setOutBusinessNo("900091009111");
        UnpaidActivityOrderEntity skuRechargeOrder = accountQuotaService.createSkuRechargeOrder(skuRechargeEntity);
        log.info("测试结果：{}", JSON.toJSONString(skuRechargeOrder));
    }

//    @Before
    public void setUp() {
        log.info("装配活动：{}", armory.assembleActivitySku(9011L));
    }

    @Test
    public void test_createSkuRechargeOrder() throws InterruptedException {
        for (int i = 0; i < 5; i++) {
            try {
                SkuRechargeEntity skuRechargeEntity = new SkuRechargeEntity();
                skuRechargeEntity.setUserId("lxd");
                skuRechargeEntity.setSku(9011L);
                // outBusinessNo 作为幂等仿重使用，同一个业务单号2次使用会抛出索引冲突 Duplicate entry '700091009111' for key 'uq_out_business_no' 确保唯一性。
                skuRechargeEntity.setOutBusinessNo(RandomStringUtils.randomNumeric(12));
                UnpaidActivityOrderEntity skuRechargeOrder = accountQuotaService.createSkuRechargeOrder(skuRechargeEntity);
                log.info("测试结果：{}", JSON.toJSONString(skuRechargeOrder));

            } catch (AppException e) {
                log.warn(e.getInfo());
            }
        }

        new CountDownLatch(1).await();
    }

    @Resource
    private IRaffleActivityPartakeService raffleActivityPartakeService;

    @Test
    public void test_createOrder() {
        // 请求参数
        PartakeRaffleActivityEntity partakeRaffleActivityEntity = new PartakeRaffleActivityEntity();
        partakeRaffleActivityEntity.setUserId("lxd");
        partakeRaffleActivityEntity.setActivityId(100301L);
        // 调用接口
        UserRaffleOrderEntity userRaffleOrder = raffleActivityPartakeService.createPartakeRaffleActivityOrder(partakeRaffleActivityEntity);
        log.info("请求参数：{}", JSON.toJSONString(partakeRaffleActivityEntity));
        log.info("测试结果：{}", JSON.toJSONString(userRaffleOrder));
    }

    @Test
    public void test_createSkuRechargeOrder_duplicate() {
        SkuRechargeEntity skuRechargeEntity = new SkuRechargeEntity();
        skuRechargeEntity.setUserId("lxd");
        skuRechargeEntity.setSku(9011L);
        // outBusinessNo 作为幂等仿重使用，同一个业务单号2次使用会抛出索引冲突 Duplicate entry '700091009111' for key 'uq_out_business_no' 确保唯一性。
        skuRechargeEntity.setOutBusinessNo("700091009117");
        // skuRechargeEntity.setOrderTradeType(OrderTradeTypeVO.rebate_no_pay_trade);
        skuRechargeEntity.setOrderTradeType(OrderTradeTypeVO.credit_pay_trade);
        UnpaidActivityOrderEntity skuRechargeOrder = accountQuotaService.createSkuRechargeOrder(skuRechargeEntity);
        log.info("测试结果：{}", JSON.toJSONString(skuRechargeOrder));

    }

    @Test
    public void test_createOrder_pay() throws InterruptedException {
        TradeEntity tradeEntity = new TradeEntity();
        tradeEntity.setUserId("lxd");
        tradeEntity.setTradeName(TradeNameVO.CONVERT_SKU);
        tradeEntity.setTradeType(TradeTypeVO.REVERSE);
        tradeEntity.setAmount(new BigDecimal("-1.68"));
        tradeEntity.setOutBusinessNo("700091009117");
        creditAdjustService.createCreditOrder(tradeEntity);

        new CountDownLatch(1).await();
    }

    @Test
    public void test_queryCreditAccount() {
        activityService.queryUserCreditAccount("lxd");
    }

    @Test
    public void test_querySkuProduct() {
        activityService.querySkuProductListByActivityId(100301L);
    }

    @Test
    public void test_paySku() {
        SkuProductShopCartRequestDTO resquest = new SkuProductShopCartRequestDTO();
        resquest.setSku(9011L);
        resquest.setUserId("lxd");
        activityService.creditPayExchangeSku(resquest);
    }
}
